﻿@* 应用层Web Api项目入口程序 *@
@using Util.Generators.Helpers
@model Util.Generators.Contexts.EntityContext
@{
    var service = new GenerateService( Model );
    Model.Output.FileNameNoExtension = "Program";
    Model.Output.Extension = ".cs";
    Model.Output.RelativeRootPath = $"src/{service.GetWebApiProjectName()}";
}
using Util;
using @service.GetWebApiNamespace();
using @service.GetDataNamespace();
using @service.GetDataNamespace(Model.ProjectContext.TargetDbType);

//创建Web应用程序生成器
var builder = WebApplication.CreateBuilder( args );

//配置服务
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpLogging( options => options.LoggingFields = HttpLoggingFields.All );

//配置数据库迁移服务
builder.Services.AddHostedService<MigrationHostedService>();

//配置Cors
builder.Services.AddCors( options => options.AddPolicy( "cors", policy => {
    policy.SetIsOriginAllowed( _ => true );
    policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials();
} ) );

//配置Util
builder.AsBuild()
    @if ( service.Utc() ) {
    @:.AddUtc()
    }
    .AddSerilog()
    .@service.AddUnitOfWork()<@service.GetIUnitOfWork(),@service.GetUnitOfWork()>( builder.Configuration.GetConnectionString( "DefaultConnection" ) )
    .AddUtil();

//构建Web应用程序
var app = builder.Build();

//配置请求管道
if ( app.Environment.IsDevelopment() )
    app.UseDeveloperExceptionPage();
if ( app.Environment.IsDevelopment() ) {
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpLogging();
app.UseCors( "cors" );
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();

//运行应用
app.Run();
